﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace WenSkin.Controls.Paging
{
    public class WenDataTablePaging : WenPaging
    {
        public WenDataTablePaging() : base()
        {
        }

        #region 公有属性

        private PagingPageCollection items;
        private object dataSource;

        #endregion

        #region 公有属性

        [Category("Wen"), Description("获取或设置每一页设置数量数量"), DefaultValue(100)]
        public int PageRowCount { get; set; } = 100;

        [Category("Wen"), Description("获取设置单项数据"), DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(Design.Editor.WenCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public PagingPageCollection Items => items ??= new PagingPageCollection(this);
        public object DataSource { get => dataSource; set { dataSource = value; DataBind(); } }

        #endregion

        private void DataBind()
        {
            Items.Clear();
            if (dataSource is DataTable dt)
            {
                //向上取整
                MaxPageCout = (int)Math.Ceiling((decimal)dt.Rows.Count / PageRowCount);
                for (int i = 1; i <= MaxPageCout; i++)
                {
                    List<DataRow> dataRows = new List<DataRow>();
                    for (int r = PageRowCount * (i - 1); r < (i * PageRowCount > dt.Rows.Count ? dt.Rows.Count : i * PageRowCount); r++)
                    {
                        dataRows.Add(dt.Rows[r]);
                    }
                    PagingPage paging = new PagingPage()
                    {
                        PageIndex = i,
                        Value = dataRows.ToArray(),
                    };
                    Items.Add(paging);
                }
                OnPagingButtonClick(new PagingButtonClickEventArgs(1));
            }
        }

        #region 重写

        protected override void OnPagingButtonClick(PagingButtonClickEventArgs e)
        {
            base.OnPagingButtonClick(e);
            PagingPageChange?.Invoke(this, new PagingPageChangeEventArgs(e.PageIndex, Items[e.PageIndex-1]));
        }

        #endregion

        #region 委托

        public delegate void PagingPageChangeEventHandler(object sender, PagingPageChangeEventArgs e);
        [Category("Wen"), Description("绑定数据值变化事件")]
        public event PagingPageChangeEventHandler PagingPageChange;
        public class PagingPageChangeEventArgs : PagingButtonClickEventArgs
        {
            private readonly PagingPage pagingPage;

            public PagingPageChangeEventArgs(int pageIndex, PagingPage pagingPage) : base(pageIndex)
            {
                this.pagingPage = pagingPage;
            }
            public PagingPage PagingPage => pagingPage;
        }

        #endregion  

        #region 子类

        [ToolboxItem(false)]
        [DesignTimeVisible(false)]
        public class PagingPage : Component
        {
            public object Value { get; set; }
            public int PageIndex { get; set; }
            public DataRow[] DataRows => Value as DataRow[];
            public DataTable DataTable
            {
                get
                {
                    DataTable dt = DataRows[0].Table.Clone();
                    foreach (DataRow dataRow in DataRows)
                    {
                        dt.Rows.Add(dataRow.ItemArray);
                    }
                    return dt;
                }
            }
            public override string ToString()
            {
                return PageIndex.ToString();
            }
        }

        public class PagingPageCollection : WenCollection
        {
            public PagingPageCollection(WenPaging owner)
            {
                this.owner = owner;
            }

            #region 私有属性
            private WenPaging owner;
            #endregion
            public PagingPage this[int index] => index < Items.Count ? Items[index] as PagingPage : null;
        }
        #endregion
    }
}
